package cn.fl.customer.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.service.ICstNpSocialRlService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.customer.vo.CstNpSocialRlVO;
import cn.fl.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.condition.CstNpBscInfoQueryCondition;
import fl.customer.facade.customer.condition.CstNpSocialRlQueryCondition;
import fl.customer.facade.customer.dto.CstNpBscInfoDTO;
import fl.customer.facade.customer.dto.CstNpSocialRlDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 自然人客户基础信息表 控制层
 */
@Controller
@RequestMapping("/customer/cstNpBscInfo")
public class CstNpBscInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CstNpBscInfoController.class);

	/**
	 * CstNpBscInfo 服务层BEAN
	 */
	@Autowired
	private ICstNpBscInfoService cstNpBscInfoService;
	/**
	 * CstNpSocialRl 服务层BEAN
	 */
	@Autowired
	private ICstNpSocialRlService cstNpSocialRlService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		logger.info("加载CstNpBscInfo列表页面.");
		ModelAndView view = new ModelAndView("customer/cstNpBscInfo/list");
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		logger.info("加载CstNpBscInfo Modify页面.");
		ModelAndView view = new ModelAndView("customer/cstNpBscInfo/modify");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		CstNpBscInfoQueryCondition qc = new CstNpBscInfoQueryCondition();
		page.setCondition(qc);

		logger.info("==初始化列表数据.==");
		Pager pager = new Pager();//this.cstNpBscInfoService.searchList(page);
		List<CstNpBscInfoDTO> list = (List<CstNpBscInfoDTO>) pager.getResultList();
		ListPageVO<CstNpBscInfoDTO> listPage = new ListPageVO<CstNpBscInfoDTO>(list, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			//this.cstNpBscInfoService.removeById(Long.valueOf(id));
			logger.debug("==remove==id:" + id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("删除失败");
			logger.error("删除失败!", ex);
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		
		try {
			CstNpBscInfoVO vo = this.cstNpBscInfoService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在！");
			}
			result.setData(vo);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取数据失败！");
			logger.error("获取数据失败！", ex);
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		//String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		try {
			//CstNpBscInfoVO entity = new CstNpBscInfoVO();
			Long id = null;//this.cstNpBscInfoService.add(entity);
			result.setData(id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
			logger.error("", ex);
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		//String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			CstNpBscInfoVO entity = new CstNpBscInfoVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			boolean success = false;//this.cstNpBscInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败！");
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("修改失败!");
			logger.error("修改失败!", ex);
		}
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyCstNpSocialRlForSpouse.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyCstNpSocialRlForSpouse(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String cstId = request.getParameter("cstId");
		String provCd = request.getParameter("provCd");// 承租人实际区域-省
		String genderCd = request.getParameter("genderCd");// 承租人性别
		String marStsCd = request.getParameter("marStsCd");// 承租人婚姻状况（CST0007）
		String rlNm = request.getParameter("rlNm");// 配偶姓名
		String mpMate = request.getParameter("mpMate");// 配偶电话
		String certNoMate = request.getParameter("certNoMate");// 配偶身份证
		String idAddr = request.getParameter("idAddr");// 身份证地址
		String postAddr = request.getParameter("postAddr");// 通讯地址
		String workPlc = request.getParameter("workPlc");// 工作单位
		String workAddr = request.getParameter("workAddr");// 单位地址
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"修改成功!");
		if(StringUtils.isEmpty(cstId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			if(StringUtils.isEmpty(idAddr)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("身份证地址不能为空！");
				return result;
			}
			if(StringUtils.isEmpty(postAddr)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("通讯地址不能为空！");
				return result;
			}
			// if(StringUtils.isEmpty(workPlc)){
			// 	result.setSuccess(BaseConstant.IS_YESNO_NO);
			// 	result.setInfo("工作单位不能为空！");
			// 	return result;
			// }
			if(StringUtils.isEmpty(workAddr)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("单位地址不能为空！");
				return result;
			}
			CstNpBscInfoVO entity = new CstNpBscInfoVO();
			entity.setId(Long.valueOf(cstId));
			entity.setMarStsCd(marStsCd);
			entity.setIdAddr(idAddr);
			entity.setPostAddr(postAddr);
			// entity.setWorkPlc(workPlc);
			entity.setWorkAddr(workAddr);
			boolean modify = this.cstNpBscInfoService.modify(entity);
			if(!modify){
				logger.info("修改失败！modify");
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败！");
				return result;
			}
			boolean modifyFlag = false; // 是否保存配偶信息
			if(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_MAR_STS_CD_2).equals(marStsCd)
				|| PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_MAR_STS_CD_FIRST).equals(marStsCd)
					|| PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_MAR_STS_CD_AGIAN).equals(marStsCd)
					|| PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_MAR_STS_CD_BACK).equals(marStsCd)){
				modifyFlag = true;
			}
			CstNpSocialRlVO cstNpSocialRlVO = new CstNpSocialRlVO();
			cstNpSocialRlVO.setCstId(Long.valueOf(cstId));
			cstNpSocialRlVO.setRlTypCd(DictionaryConstant.CODE_GRP_RL_TYP_CD_5);

			if(modifyFlag) {
				cstNpSocialRlVO.setIsDel(BaseConstant.IS_YESNO_NO);
				if(StringUtils.isEmpty(rlNm)){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("配偶姓名不能为空！");
					return result;
				}
				if(StringUtils.isEmpty(mpMate)){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("配偶电话不能为空！");
					return result;
				}
				cstNpSocialRlVO.setRlNm(rlNm);
				cstNpSocialRlVO.setMp(mpMate);
				// cstNpSocialRlVO.setCertNo(certNoMate);
				cstNpSocialRlVO.setCertTypCd("0");
				if(StringUtils.isNotEmpty(genderCd)){
					String GenderMale = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_GENDER_CD_1);/**性别组值（SYS0004）男 */
					String GenderFemale = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_GRP_GENDER_CD_2);/**性别组值（SYS0004）女 */
					cstNpSocialRlVO.setGenderCd(GenderMale.equals(genderCd.trim()) ? GenderFemale : GenderMale);
				}

				boolean success = this.cstNpBscInfoService.modifyCstNpSocialRlForSpouse(cstNpSocialRlVO);
				if(!success){
					logger.info("修改失败！modifyCstNpSocialRlForSpouse");
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("修改失败！");
				}
			}else{
				CstNpSocialRlQueryCondition condition = new CstNpSocialRlQueryCondition();
				List<Long> cstIds = new ArrayList<>();
				cstIds.add(Long.valueOf(cstId));
				condition.setCstIdList(cstIds);
				condition.setRlTypCd(DictionaryConstant.CODE_GRP_RL_TYP_CD_5);
				List<CstNpSocialRlDTO> cstNpSocialRlDTOS = this.cstNpSocialRlService.searchList(condition);
				if(CollectionUtils.isNotEmpty(cstNpSocialRlDTOS)){
					List<Long> ids = new ArrayList<>();
					ids.add(cstNpSocialRlDTOS.get(0).getId());
					boolean b = this.cstNpSocialRlService.removeByIds(ids);
					if(!b){
						logger.info("修改失败！modifyCstNpSocialRlForSpouse");
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("修改失败！");
					}
				}
			}

		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("修改失败!");
			logger.error("修改失败!", ex);
		}
		return result;
	}

}

